//
//  StatusOriginalView.swift
//  0110-weibo
//
//  Created by 鲍志刚 on 16/1/18.
//  Copyright © 2016年 zhongye. All rights reserved.
//

import UIKit
import SnapKit

class StatusOriginalView: UIView {
    
    var status: Status? {
        
        didSet{
        //绑定数据
            headImage.sd_setImageWithURL(status?.user?.headImageURL, placeholderImage: UIImage(named: "avatar_default_big"))
            nameLabel.text = status?.user?.name
            mbrankImage.image = status?.user?.mbrank_image
            verifiedTypeImage.image = status?.user?.verified_type_image
            timeLabel.text = status?.created_at
            sourceLabel.text = status?.source
            contentLabel.text = status?.text
            
            //传递 配图视图的数据源
        pictureView.imageURLs = status?.imageURLs
            
            
            //设置一个非常关键的属性 就可以实现 视图的行高自动获取
//            self.snp_makeConstraints { (make) -> Void in
//                make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
//            }
        
        
        
        
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //添加子视图
        setupUI()

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI (){
        
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        
//        backgroundColor = UIColor.purpleColor()
        //添加所有子视图
        addSubview(headImage)
        addSubview(nameLabel)
        addSubview(mbrankImage)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedTypeImage)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //设置约束StatusCellMargin        
        headImage.snp_makeConstraints { (make) -> Void in
            make.top.left.equalTo(self.snp_top).offset(StatusCellMargin)
            make.width.height.equalTo(StatusCellImageWidth)
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(headImage.snp_right).offset(StatusCellMargin)
            make.top.equalTo(headImage.snp_top)
        }
        
        mbrankImage.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
            make.top.equalTo(nameLabel.snp_top)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(headImage.snp_bottom)
            make.left.equalTo(nameLabel.snp_left)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
            make.bottom.equalTo(timeLabel.snp_bottom)
        }
        
        //微博认证
        verifiedTypeImage.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImage.snp_right)
            make.centerY.equalTo(headImage.snp_bottom)
        }
        
        //微博正文
        contentLabel.snp_makeConstraints { (make) -> Void in
             //设置一个非常关键的属性 就可以实现 视图的行高自动获
        make.top.equalTo(headImage.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(headImage.snp_left)
        }
        
        //微博配图
        pictureView.snp_makeConstraints { (make) -> Void in
            //iOS 9之后 必须是 left -> left  right -> right
            make.left.equalTo(headImage.snp_left)
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
            
            //TODO: 后续修改 先给定测试大小
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        
        // 设置一个非常关键的属性 就可以实现 视图的行高自动获取
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin)
        }
    }
    
    //懒加载所有子视图
    private lazy var headImage : UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    private lazy var nameLabel : UILabel = UILabel(title: "隔壁老王", fontSize: 14, color: themeColor)
    
    //用户等级
    private lazy var mbrankImage: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //时间
    private lazy var timeLabel = UILabel(title: "22:22", fontSize: 14, color: UIColor.lightGrayColor())

    //微博来源
    private lazy var sourceLabel: UILabel = UILabel(title: "22期的同学们", fontSize: 14, color: UIColor.lightGrayColor())
    
    //微博认证类型
    private lazy var verifiedTypeImage : UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //微博正文
    private lazy var contentLabel: UILabel = UILabel(title: "听说吃着麻辣烫听着音乐才是生活", fontSize: 14, color: UIColor.darkGrayColor(), margin: StatusCellMargin)
    
    //懒加载视图
    private lazy var pictureView: StatusCellPictureView =  StatusCellPictureView()
}
